package com.liangsui.mss.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.component.SmsComponent;
import com.liangsui.mss.constants.AuthServerConstant;
import com.liangsui.mss.domain.VerificationCode;
import com.liangsui.mss.mapper.VerificationCodeMapper;
import com.liangsui.mss.service.VerificationCodeService;
import com.liangsui.mss.utils.IdGeneratorSnowflake;
import com.liangsui.mss.vo.AjaxResult;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

/**
* @author zhang
* @description 针对表【his_verification_code(手机验证码表)】的数据库操作Service实现
* @createDate 2022-12-03 18:45:20
*/
@DubboService(timeout = 5000, methods = {@Method(name = "sendSms", retries = 0)})
public class VerificationCodeServiceImpl extends ServiceImpl<VerificationCodeMapper, VerificationCode> implements VerificationCodeService{

    @Autowired
    private SmsComponent smsComponent;

    @Autowired
    private VerificationCodeMapper verificationCodeMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public int sendSms(String phoneNumber) {
        try {
            //验证码防刷
            String redisCode = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_PREFIX + phoneNumber);
            if (StringUtils.isNotEmpty(redisCode)) {
                long l = Long.parseLong(redisCode.split("_")[1]);
                if (System.currentTimeMillis() - l < 60000) {
                    //60秒内不得重发
                    return 0;
                }
            }

            Integer verificationCode = IdGeneratorSnowflake.generateVerificationCode();
            smsComponent.sendSmsCode(phoneNumber, String.valueOf(verificationCode));
            //redis缓存验证码
            String code = verificationCode + "_" + System.currentTimeMillis();
            redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_PREFIX + phoneNumber, code, 5, TimeUnit.MINUTES);
            return saveVerification(phoneNumber, verificationCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public VerificationCode checkCode(String phoneNumber, Integer code) {
        QueryWrapper<VerificationCode> wrapper = new QueryWrapper<>();
        wrapper.eq(VerificationCode.COL_PHONE_NUMBER, phoneNumber);
        wrapper.eq(VerificationCode.COL_VERIFICATION_CODE, code);
        wrapper.eq(VerificationCode.COL_IS_CHECK, 0);
        return verificationCodeMapper.selectOne(wrapper);
    }

    private int saveVerification(String phoneNumber, Integer verificationCode) {
        VerificationCode code = new VerificationCode();
        code.setPhoneNumber(phoneNumber);
        code.setVerificationCode(verificationCode);
        code.setCreateTime(DateUtil.date());
        code.setIsCheck(0);
        return verificationCodeMapper.insert(code);
    }
}




